using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.DirectX;

namespace GraphicsAPIs
{
    public class TrackBall
    {
        private double _ballRadius;

        public double BallRadius
        {
            get { return _ballRadius; }
            set { _ballRadius = value; }
        }

        public Vector3 ProjectOnSphere(Vector2 vector)
        {
            float rsqr = (float)(BallRadius * BallRadius);
            float dsqr = vector.LengthSq();
            Vector3 v = new Vector3();

	        if(dsqr>rsqr)
	        {
                float scale = (float)((BallRadius - .05) / Math.Sqrt(dsqr));
                v.X = vector.X * scale;
                v.Y = vector.Y * scale;
                v.Z = 0;
	        }
	        else
	        {
                v.X = vector.X;
                v.Y = vector.Y;
                v.Z = (float)(Math.Sqrt(rsqr - dsqr));
	        }
            return v;
        }
    }
}
